var BaseUserInvitationSelectf = app.controller('BaseUserInvitationSelectf', function($rootScope, $scope, $http,$state,BaseUserInvitationSelectfData,BaseUserInvitationSelectfInfo) {
	try{
		$rootScope.showf=false;
		$scope.all=true;//是否全部选中

		$scope.mm=0;//总金额
		$scope.cc=0;//总人数
		$scope.show=false;

		$scope.user=BaseUserInvitationSelectfInfo;
		if($scope.user.phone_tel==undefined||$scope.user.phone_tel==null||$scope.user.phone_tel==""){
			if($rootScope.routefrom=="back"){
				$rootScope.go("ucenter");
			}else{
				$rootScope.go("telmodify");
			}
			return;
		}

		$scope.url=SITE_URL+"index.php?app=download&f="+$scope.user.phone_tel;

		$scope.clist=[];//通讯录列表

		$scope.temp=[];

		var options = new ContactFindOptions();  
		var fields = ["displayName", "phoneNumbers"];  
		options.multiple = true;  

		$scope.timer = null;
		navigator.contacts.find(fields,function(contacts) {  
			try{
				ProgressIndicator.showSimpleWithLabel(false, '正在加载通讯录...');
				$scope.timer = window.setTimeout("ProgressIndicator.hide()", 10000);

				if(contacts==undefined||contacts==null||contacts==""||contacts.length<=0){
					ProgressIndicator.hide();
					return false;
				}

				console.log('Found ' + contacts.length + ' contacts.');  
				for(var i =0; i < contacts.length; i++){  
					if(contacts[i].phoneNumbers!=undefined&&contacts[i].phoneNumbers!=null&& contacts[i].phoneNumbers.length>0){
						var obj = {};
						obj.displayName=contacts[i].displayName; 
						obj.phoneNumber= contacts[i].phoneNumbers[0].value;
						obj.h=1;

						var reg1 = /(^13\d{9}$)|(^14)[5,7]\d{8}$|(^15[0,1,2,3,5,6,7,8,9]\d{8}$)|(^17)[6,7,8]\d{8}$|(^18\d{9}$)/g ;  
						if(reg1.test(obj.phoneNumber)){
							$scope.temp.push(obj);
						} 
					}
				}

				var g= JSON.stringify($scope.temp);
				var url=SITE_URL+"index.php?app=baseloginapp&callback=JSON_CALLBACK&act=contactcompare";
				$http.post(url,{g:g},{'Content-Type':'application/x-www-form-urlencoded'}).success(function(data){

					if ($scope.timer != null)
						window.clearTimeout($scope.timer);
					
					$scope.clist = data.retval;

					$scope.cc=$scope.clist.length;
					$scope.mm=parseInt($scope.cc)*20;
					ProgressIndicator.hide();
				}).error(function () {
					if ($scope.timer != null)
						window.clearTimeout($scope.timer);
					ProgressIndicator.hide();
				});
			}
			catch(e){ if ($scope.timer != null)
				window.clearTimeout($scope.timer);
			ProgressIndicator.hide();showcatch(e);}

		}, function onError(contactError) {  
			if ($scope.timer != null)
				window.clearTimeout($scope.timer);
			ProgressIndicator.hide(); 
		},options); 

		//子项选择
		$scope.itemclick=function(s){
			if($scope.clist!=undefined&&$scope.clist.length>0){
				$scope.clist[s.$index].h=Math.abs(Math.abs($scope.clist[s.$index].h)-1);

				$scope.cc=0;
				$scope.mm=0;
				for ( var i = 0; i < $scope.clist.length; i++) {						
					if($scope.clist[i].h>0){
						console.log($scope.cc);
						$scope.cc=$scope.cc+1;
					}
				}
				$scope.mm=$scope.cc*20;
			}
			console.log($scope.cc);
			console.log($scope.mm);
		}

		//全选
		$scope.selectall=function(){
			$scope.all=!$scope.all;
			$scope.cc=0;
			$scope.mm=0;
			if($scope.clist!=undefined&&$scope.clist.length>0){
				for ( var i = 0; i < $scope.clist.length; i++) {
					if($scope.all){
						$scope.clist[i].h=1;
					}else{
						$scope.clist[i].h=0;
					}
					if($scope.clist[i].h>0){
						$scope.cc=$scope.cc+1;
					}
				}
				$scope.mm=$scope.cc*20;
			}
		}

		$scope.send=function(){
			$scope.show=!$scope.show;
		}

		$scope.busy=false;
		$scope.done=function(){
			try{
				if($scope.busy){
					return false;
				}
				if($scope.cc<=0){
					//
					showtoast('请选择一位朋友后邀请');
					return false;
				}
				$scope.busy=true;
				var temp=[];
				for ( var i = 0; i < $scope.clist.length; i++) {

					if($scope.clist[i].h>0){
						temp.push($scope.clist[i]);
						$scope.clist.splice(i,1);
					}
				}
				ProgressIndicator.showSimpleWithLabel(false,'发送中...');
				//var g= JSON.stringify($scope.clist);
				//var url=SITE_URL+"index.php?app=baseloginapp&callback=JSON_CALLBACK&act=contactdone&g="+g;
				//$http.jsonp(url).success(function(data) {
				//if(data.done){
				//$scope.show=!$scope.show;
				//showtoast("发送成功");
				//}
				//$scope.busy=false;
				//ProgressIndicator.hide();
				//});

//				var temp=[];
//				for ( var i = 0; i < $scope.clist.length; i++) {
//				if($scope.clist[i].h>0){
//				temp.push($scope.clist[i]);
//				}
//				}
				var g= JSON.stringify(temp);
				var url=SITE_URL+"index.php?app=baseloginapp&callback=JSON_CALLBACK&act=contactdone&tel="+$scope.user.phone_tel;
				$http.post(url,{g:g},{'Content-Type':'application/x-www-form-urlencoded'}).success(function(data){
					$scope.show=!$scope.show;
					showtoast("发送成功");
					$scope.busy=false;
					ProgressIndicator.hide();
				});
			}catch(e){

			}
		}

		$scope.wxlogintimer = null;
		$scope.sharew = function(){			
			try{
				ProgressIndicator.showSimpleWithLabel(false,'正在启动微信...');
				var url=SITE_URL+"index.php?app=baseloginapp&callback=JSON_CALLBACK&act=userinfo";
				$http.jsonp(url).success(function(data) {
					var link = SITE_URL+"index.php?app=download&f="+data.retval.phone_tel;
					$scope.wxlogintimer = window.setTimeout("ProgressIndicator.hide()", 10000);
					Wechat.share({
						type: Wechat.Type.WEBPAGE,
						message:{
							title: '恭喜你！',
							description: 'Hi，i创小伙伴送了您100元咨询现金券。10元问导师，成功近咫尺！【i创无忧】',
							url: link,
							thumbData:SITE_URL+"images/idea.png"
						},
						scene:Wechat.Scene.SESSION
					}, 
					function () {
						showtoast("分享成功");
						if ($scope.wxlogintimer != null)
							window.clearTimeout($scope.wxlogintimer);
						ProgressIndicator.hide();
					}, function (reason) {
						// 分享失败
						showtoast("分享失败");
						if ($scope.wxlogintimer != null)
							window.clearTimeout($scope.wxlogintimer);
						ProgressIndicator.hide();
						console.log(reason);
					}
					);
				}).error(function(){
					showtoast("分享失败");
					ProgressIndicator.hide();
				});
			}catch(e){showcatch(e);ProgressIndicator.hide();}
		}
	}
	catch (e) {
		showcatch(e);
	}
});
BaseUserInvitationSelectf.BaseUserInvitationSelectfData=function($q, $http,$stateParams,$rootScope){
	try {
		var data = [];
		var def = $q.defer();
		if(window.localStorage.u==undefined||window.localStorage.u==null||window.localStorage.u==""){			
			$rootScope.go("loginmain");
		}else{
			def.resolve(data);
		}

		return def.promise;
	} catch (e) {
		showcatch(e);
	}
}
BaseUserInvitationSelectf.BaseUserInvitationSelectfInfo=function($q, $http,$stateParams){
	try {
		var def = $q.defer();
		var url=SITE_URL+"index.php?app=baseloginapp&callback=JSON_CALLBACK&act=userinfo";
		$http.jsonp(url).success(function (data) {
			def.resolve(data.retval);
		});

		return def.promise;
	} catch (e) {
		showcatch(e);
	}
}